package com.crm.uilt;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
/**
 * @author zjd
 * @Title:
 * @Description:
 * @date 2021/4/617:11
 */


@Component
public class SpringUtil implements ApplicationContextAware {

    private static ApplicationContext context; // Spring应用上下文环境

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (context == null) {
            context = applicationContext;
        }
    }

    public static ApplicationContext getApplicationContext() {
        return context;
    }

    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) throws BeansException {
        return (T) context.getBean(name);
    }

    @SuppressWarnings("unchecked")
    public static <T> T getBean(Class<?> clz) throws BeansException {
        return (T) context.getBean(clz);
    }


    // 判断application是否为空
    public static void isContext() throws Exception {
        if (context == null) {
            throw new Exception("application未注入");
        }
    }

}


